//
//  BaseNavigationController.m
//  JQLive
//
//  Created by Bruce on 2019/11/26.
//  Copyright © 2019 JQLive. All rights reserved.
//

#import "BaseNavigationController.h"

@interface BaseNavigationController ()

@end

@implementation BaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];

   self.navigationBar.translucent = NO;
    // 2.滑动返回手势
//    self.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
}

#pragma mark -
#pragma mark Setter
- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle {
    _statusBarStyle = statusBarStyle;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)setTitleColor:(UIColor *)titleColor {
    NSDictionary *attributes = @{NSForegroundColorAttributeName:titleColor};
    [self.navigationBar setTitleTextAttributes:attributes];
}

- (void)setBarTintColor:(UIColor *)barTintColor {
    self.navigationBar.tintColor = barTintColor;
}

- (void)setBarBackgourndColor:(UIColor *)barBackgourndColor {
    [self.navigationBar setBackgroundImage:[self imageWithColor:barBackgourndColor] forBarMetrics:UIBarMetricsDefault];
}

- (UIImage *)imageWithColor:(UIColor *)color {
    static NSCache *imageCache;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        imageCache = [[NSCache alloc] init];
    });
    UIImage *image = [imageCache objectForKey:color];
    if (image) {
        return image;
    }
    
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [imageCache setObject:image forKey:color];
    return image;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.statusBarStyle;
}

#pragma mark 返回按钮
-(void)popself {
    [self popViewControllerAnimated:YES];
}
#pragma mark 创建返回按钮
-(UIBarButtonItem *)createBackButton {
    return [[UIBarButtonItem alloc] initWithImage:[UIImage svgImageNamed:@"nav_back" size:CGSizeMake(22, 22)] style:UIBarButtonItemStylePlain target:self action:@selector(popself)];
}
//- (void)setupNavigationItems{
//    self.navigationItem.leftBarButtonItem =[self createBackButton];
////    [super setupNavigationItems];
////
////    if (isHiddenBackButton)
////    {
////        BaseUINavigationButton *leftButton = [[BaseUINavigationButton alloc] initWithType:QMUINavigationButtonTypeImage];
////        leftButton.frame = CGRectMake(0, 0, (44), (44));
////        [leftButton setTitle:@"" forState:0];
////        leftButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
////        UIBarButtonItem * leftItem = [UIBarButtonItem qmui_itemWithButton:leftButton target:self action:nil];
////        self.navigationItem.leftBarButtonItems = @[leftItem];
////    }
//
//}
//
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.viewControllers.count) {
//        viewController.navigationItem.leftBarButtonItem =[self createBackButton];// 加了这句话 会导致 不能全局右滑返回
        viewController.hidesBottomBarWhenPushed = YES;
    }else{
        viewController.hidesBottomBarWhenPushed = NO;
    }
    [super pushViewController:viewController animated:animated];
//    if ( [self.viewControllers count] > 1) {
//        viewController.hidesBottomBarWhenPushed = YES;
//        viewController.navigationItem.leftBarButtonItem =[self createBackButton];
//    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
